package com.small.controller.backend;

import com.small.common.Const;
import com.small.common.RedisShardedPool;
import com.small.common.ResponseCode;
import com.small.common.ServerResponse;
import com.small.pojo.User;
import com.small.service.ICategoryService;
import com.small.service.IUserService;
import com.small.util.CookieUtil;
import com.small.util.JsonUtil;
import com.small.util.RedisShradedPoolUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: small
 * @description: 商品分类管理
 * @author: Mr.Xie
 * @create: 2019-04-24 19:07
 */
@Controller
@RequestMapping("/manage/category")
public class CategoryManageController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private ICategoryService iCategoryService;
    /**
     * 添加品类
    *@Param: categoryName：添加品类的名称
     *       parentId：设置父节点，默认为0表示根节点
    *@return:
    */
    @RequestMapping("add_category.do")
    @ResponseBody
    public ServerResponse addCategory(HttpServletRequest request, String categoryName, @RequestParam(value = "parentId",defaultValue = "0") int parentId){
//        String loginToken = CookieUtil.readLoginToken(request);
//        if (StringUtils.isEmpty(loginToken)) {
//            return ServerResponse.createBySuccessMessage("用户未登录");
//        }
//        String userJsonstr = RedisShradedPoolUtil.get(loginToken);
//        User user = JsonUtil.string2Obj(userJsonstr, User.class);
//        if(user==null){
//            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请登录");
//        }
//        //校验是否是管理员
//        if(iUserService.checkAdminRole(user).isSuccess()){
//            //是管理员,增加物品分类操作
//            return iCategoryService.addCategory(categoryName,parentId);
//        }else{
//            return ServerResponse.createByErrorMessage("无权限操作，需要管理员权限");
//        }
        return iCategoryService.addCategory(categoryName,parentId);
    }
    /**
     * 修改品类名称
    *@Param: categoryId：要修改的品类id
     *       categoryName:修改后的名称
    *@return:
    */
    @RequestMapping("set_category_name.do")
    @ResponseBody
    public ServerResponse setCategoryName(HttpServletRequest request,Integer categoryId,String categoryName){
//        String loginToken = CookieUtil.readLoginToken(request);
//        if (StringUtils.isEmpty(loginToken)) {
//            return ServerResponse.createBySuccessMessage("用户未登录");
//        }
//        String userJsonstr = RedisShradedPoolUtil.get(loginToken);
//        User user = JsonUtil.string2Obj(userJsonstr, User.class);
//        if(user==null){
//            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请登录");
//        }
//
//        if(iUserService.checkAdminRole(user).isSuccess()){
//            //更新categoryName
//            return iCategoryService.updateCategoryName(categoryId,categoryName);
//        }else{
//            return ServerResponse.createByErrorMessage("无权限操作，需要管理员权限");
//        }
        return iCategoryService.updateCategoryName(categoryId,categoryName);
    }
    /**
     * 查询平级子节点
    *@Param: parentId:要查询的父节点id
    *@return:
    */
    @RequestMapping("get_category.do")
    @ResponseBody
    public ServerResponse getChildrenParallelCategory(HttpServletRequest request,@RequestParam(value = "parentId",defaultValue = "0") Integer parentId){
//        String loginToken = CookieUtil.readLoginToken(request);
//        if (StringUtils.isEmpty(loginToken)) {
//            return ServerResponse.createBySuccessMessage("用户未登录");
//        }
//        String userJsonstr = RedisShradedPoolUtil.get(loginToken);
//        User user = JsonUtil.string2Obj(userJsonstr, User.class);
//        if(user==null){
//            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请登录");
//        }
//        if(iUserService.checkAdminRole(user).isSuccess()){
//            //查询子节点Category，并且不递归，保持平级
//            return  iCategoryService.getChildrenParallelCategory(parentId);
//        }else{
//            return ServerResponse.createByErrorMessage("无权限操作，需要管理员权限");
//        }
        return  iCategoryService.getChildrenParallelCategory(parentId);
    }
    /**
     * 递归查询所有子节点
    *@Param: categoryId:商品id
    *@return:
    */
    @RequestMapping("get_deep_category.do")
    @ResponseBody
    public ServerResponse getCategoryAndDeepChildrenCategory(HttpServletRequest request,@RequestParam(value = "categoryId",defaultValue = "0") Integer categoryId){
//        String loginToken = CookieUtil.readLoginToken(request);
//        if (StringUtils.isEmpty(loginToken)) {
//            return ServerResponse.createBySuccessMessage("用户未登录");
//        }
//        String userJsonstr = RedisShradedPoolUtil.get(loginToken);
//        User user = JsonUtil.string2Obj(userJsonstr, User.class);
//        if(user==null){
//            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请登录");
//        }
//        if(iUserService.checkAdminRole(user).isSuccess()){
//            //查询当前节点的id和递归子节点的id
//            return iCategoryService.selectCategoryAndChildrenById(categoryId);
//
//        }else{
//            return ServerResponse.createByErrorMessage("无权限操作，需要管理员权限");
//        }
        return iCategoryService.selectCategoryAndChildrenById(categoryId);
    }

}
